package city.spring.modules.personnel.controller;

import city.spring.modules.personnel.entity.EmployeeTrainingEntity;
import city.spring.modules.personnel.service.EmployeeTrainingService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Controller：员工培训
 *
 * @author Panxm
 * @date 2020-04-14 16:35:31
 */
@RestController
@RequestMapping("/employee_training")
public class EmployeeTrainingController extends ApiController {
    private final EmployeeTrainingService employeeTrainingService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<EmployeeTrainingEntity, ?>> entityOrderFields;

    public EmployeeTrainingController(EmployeeTrainingService employeeTrainingService) {
        this.employeeTrainingService = employeeTrainingService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(EmployeeTrainingEntity::getId);
        entityOrderFields.add(EmployeeTrainingEntity::getName);
        entityOrderFields.add(EmployeeTrainingEntity::getType);
        entityOrderFields.add(EmployeeTrainingEntity::getTeacher);
        entityOrderFields.add(EmployeeTrainingEntity::getOrg);
        entityOrderFields.add(EmployeeTrainingEntity::getAddress);
        entityOrderFields.add(EmployeeTrainingEntity::getStart);
        entityOrderFields.add(EmployeeTrainingEntity::getEnd);
        entityOrderFields.add(EmployeeTrainingEntity::getContent);
        entityOrderFields.add(EmployeeTrainingEntity::getAttachments);
        entityOrderFields.add(EmployeeTrainingEntity::getPersonType);
        entityOrderFields.add(EmployeeTrainingEntity::getPersonNum);
    }

    /**
     * 获取全部的 <strong>员工培训</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, EmployeeTrainingEntity entity) {
        LambdaQueryChainWrapper<EmployeeTrainingEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>员工培训</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, EmployeeTrainingEntity entity, Date start1, Date start2) {
        LambdaQueryChainWrapper<EmployeeTrainingEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        if (start1 != null && start2 != null) {
            lambdaQuery.ge(EmployeeTrainingEntity::getStart, start1);
            lambdaQuery.le(EmployeeTrainingEntity::getStart, start2);
        }
        Page<EmployeeTrainingEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>员工培训</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        EmployeeTrainingEntity entity = employeeTrainingService.getById(id);
        if (Objects.equals(entity.getPersonType(), "员工")) {
            // TODO 在这里获取所有关联的 employees 信息列表
            entity.setEmployees(null);
        } else {
            // TODO 在这里获取所有关联的 oldmans 信息列表
            entity.setOldmans(null);
        }
        employeeTrainingService.loadEmployees(entity);

        return success(entity);
    }

    /**
     * 添加一个 <strong>员工培训</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody EmployeeTrainingEntity entity) {
        employeeTrainingService.saveEmployeeTraining(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>员工培训</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody EmployeeTrainingEntity entity) {
        entity.setId(id);
        employeeTrainingService.updateEmployeeTraining(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>员工培训</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        employeeTrainingService.deleteEmployeeTraining(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>员工培训</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        employeeTrainingService.deleteEmployeeTraining(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<EmployeeTrainingEntity> buildLambdaQuery(EmployeeTrainingEntity entity) {
        LambdaQueryChainWrapper<EmployeeTrainingEntity> lambdaQuery = employeeTrainingService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), EmployeeTrainingEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getName()), EmployeeTrainingEntity::getName, entity.getName());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getType()), EmployeeTrainingEntity::getType, entity.getType());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getTeacher()), EmployeeTrainingEntity::getTeacher, entity.getTeacher());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOrg()), EmployeeTrainingEntity::getOrg, entity.getOrg());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getAddress()), EmployeeTrainingEntity::getAddress, entity.getAddress());
        lambdaQuery.like(entity.getStart() != null, EmployeeTrainingEntity::getStart, entity.getStart());
        lambdaQuery.like(entity.getEnd() != null, EmployeeTrainingEntity::getEnd, entity.getEnd());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getContent()), EmployeeTrainingEntity::getContent, entity.getContent());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getAttachments()), EmployeeTrainingEntity::getAttachments, entity
                .getAttachments());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPersonType()), EmployeeTrainingEntity::getPersonType, entity.getPersonType());
        lambdaQuery.like(entity.getPersonNum() != null, EmployeeTrainingEntity::getPersonNum, entity.getPersonNum());
        return lambdaQuery;
    }

}
